package org.pisces.wechat.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.ConnectTimeoutException;
import org.pisces.wechat.commons.button.Button;
import org.pisces.wechat.commons.button.ClickButton;
import org.pisces.wechat.commons.button.ComplexButton;
import org.pisces.wechat.commons.button.Menu;
import org.pisces.wechat.commons.button.SimpleButton;
import org.pisces.wechat.commons.button.ViewButton;
import org.pisces.wechat.commons.utils.HttpClientUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;

public class WchatUtils {


	
	public static void main(String[] args) throws ConnectTimeoutException, SocketTimeoutException, Exception {
//		createTags();
//		batchtaggingMembers();
	//	createMenu();
//		getAccessToken();
//		getTag();
//		batchuntagging();
		
//		getMenu();
//		delconditional();
//		delMenu();
//		getKflist();
//		addKfaccount();
//      inviteworker();
//      createKfSession();
//		closeKfSession();
		
	//	sendTemplate();
		
		batchgetMaterial();
	}
	
	
	
	public static void post(String path,String json) throws Exception{
		
//		String token = getAccessToken();
		String token ="9_kmMRDGWgB3Ke4a8UALjJHfNLM5yIkr0NmOSTGVDIHKNRMFzGJ0KQzpvL-2vVGC0krT-XJq1-mg3fjhqgc6DbpNs2Afr7rbewOOK40l1bL7VbXWYH2W1mF5iGCGJ2lSN4YaFCb8NJ78D2EmJREJXaAEASXL";
		
		path+="?access_token="+token;
		URL url=new URL(path);
		HttpURLConnection http = (HttpURLConnection)url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");
		http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		http.connect();
		OutputStream os = http.getOutputStream();
		if(StringUtils.isNotBlank(json)){
			os.write(json.getBytes("UTF-8"));
		}
		os.close();
		
		InputStream is = http.getInputStream();
		int size = is.available();
		byte[] bt = new byte[size];
		is.read(bt);
		String message=new String(bt,"UTF-8");
		JSONObject jsonMsg = JSONObject.parseObject(message);
		System.out.println(jsonMsg);
	}
	
	/**
	 * 获取token
	 * @since 1.0 
	 * @return
	 * @throws Exception
	 * <br><b>作者： @author 宋旋 <xuan.song@utsoft.cn></b>
	 * <br>创建时间：2017年8月7日 下午5:32:31
	 */
	public static String  getAccessToken() throws Exception{
		
		String result = HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx66e4e106a9a621f8&secret=32f1b0ab4f521dc741c3844b2e4d79c9");
		JSONObject json = JSONObject.parseObject(result);
		System.out.println(json);
		return json.getString("access_token");
	}
	
	/**
	 * 创建标签
	 * @since 1.0 
	 * @throws ConnectTimeoutException
	 * @throws SocketTimeoutException
	 * @throws Exception
	 * <br><b>作者： @author 宋旋 <xuan.song@utsoft.cn></b>
	 * <br>创建时间：2017年8月7日 下午5:32:38
	 */
	public static void  createTags() throws ConnectTimeoutException, SocketTimeoutException, Exception{
		JSONObject json = new JSONObject();
		JSONObject name = new JSONObject();
		name.put("name", "测试标签");
		json.put("tag", name);
		post("http://api.weixin.qq.com/cgi-bin/tags/create", json.toJSONString());
	}
	
	
	public static void getTag() throws Exception {
		
		String result = HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/tags/get?access_token=nMZAdivlAiKlkcAeFYv16L06gXB1NQYmI6leMvquPj0mAyB2-lBujwQaVYa33oiMjKZghG4pVQRQrVXI-bYX3k_aLUh1OxF5SpkZJVsI30RZhTI8XMWi18sRz4cfBa54WDUfAJARYP");
		System.out.println(result);
	}
	
	
	
	/**
	 * 为用户打标签
	 * @throws Exception 
	 * @since 1.0 
	 * <br><b>作者： @author 宋旋 <xuan.song@utsoft.cn></b>
	 * <br>创建时间：2017年8月7日 下午5:35:25
	 */
	public static void batchtaggingMembers() throws Exception{
		JSONObject json = new JSONObject();
		JSONArray openidList = new JSONArray();
		openidList.add("oMNpa1u91VFvEXuCayn8ssR4uGXI");
		json.put("tagid", 100);
		json.put("openid_list", openidList);
		post("http://api.weixin.qq.com/cgi-bin/tags/members/batchtagging", json.toJSONString());
	}
	
	
	/**
	 * 给用户删除标签
	 * @since 1.0 
	 * @throws Exception
	 * <br><b>作者： @author 宋旋 <369178133@></b>
	 * <br>创建时间：2017年11月30日 下午6:15:11
	 */
	public static void batchuntagging() throws Exception {
		
		JSONObject json = new JSONObject();
		JSONArray openidList = new JSONArray();
		openidList.add("oMNpa1u91VFvEXuCayn8ssR4uGXI");
		json.put("tagid", 100);
		json.put("openid_list", openidList);
		post("https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging", json.toJSONString());
		

	}
	
	public static void addconditionalMenu() throws Exception{
		JSONObject data = new JSONObject();
		Menu menu= new Menu();
		List<Button> button = new ArrayList<>();
		ClickButton clickButton = new ClickButton();
		clickButton.setKey("bind");
		clickButton.setName("101用户");
		button.add(clickButton);
		
		
		
		ViewButton viewButton = new ViewButton();
		viewButton.setUrl("http://www.lan-x.cn");
		viewButton.setName("QQ用户菜单");
		button.add(viewButton);
		
		
		ComplexButton complexButton = new ComplexButton();
		List<SimpleButton> sub_button = new ArrayList<>();
		complexButton.setName("更多帮助");
		ClickButton about = new ClickButton();
		about.setKey("about");
		about.setName("关于我们");
		sub_button.add(about);
		
		ClickButton cooperation  = new ClickButton();
		cooperation.setKey("cooperation");
		cooperation.setName("联系合作");
		sub_button.add(cooperation);
		
		
		ClickButton feedBack = new ClickButton();
		feedBack.setKey("feedBack");
		feedBack.setName("咨询反馈");
		sub_button.add(feedBack);
		
		
		complexButton.setSub_button(sub_button);
		button.add(complexButton);
		
		menu.setButton(button);
		data.put("button", button);
		
		JSONObject matchrule = new JSONObject();
		
		matchrule.put("tag_id", "101");
		data.put("matchrule", matchrule);
		
		post("http://api.weixin.qq.com/cgi-bin/menu/addconditional", data.toJSONString());
		
	}
	
	
	public static void createMenu() throws Exception{
		JSONObject data = new JSONObject();
		Menu menu= new Menu();
		List<Button> button = new ArrayList<>();
		ViewButton viewButton = new ViewButton();
		viewButton.setUrl("http://www.lan-x.cn/wchat/oauth");
		viewButton.setName("个人中心");
		button.add(viewButton);
		
		ComplexButton download = new ComplexButton();
		download.setName("下载中心");
		List<SimpleButton> sub_button = new ArrayList<>();
		
		ViewButton game = new ViewButton();
		game.setUrl("https://www.lan-x.cn/?s=mp");
		game.setName("懒熊斗地主");
		sub_button.add(game);
		
		ViewButton guild = new ViewButton();
		guild.setUrl("https://g.lan-x.cn/?s=mp");
		guild.setName("懒熊公会");
		sub_button.add(guild);
		
		download.setSub_button(sub_button);
		
		button.add(download);
		//懒熊斗地主  懒熊公会
		
		ComplexButton complexButton = new ComplexButton();
		
		sub_button = new ArrayList<>();
		complexButton.setName("帮助中心");
		ViewButton about = new ViewButton();
		about.setUrl("http://blog.lan-x.cn/archives/127");
		about.setName("新人红包");
		sub_button.add(about);
		
		ViewButton invite = new ViewButton();
		invite.setUrl("http://blog.lan-x.cn/archives/6");
		invite.setName("推荐有奖");
		sub_button.add(invite);
		
		ViewButton stove = new ViewButton();
		stove.setUrl("http://blog.lan-x.cn/archives/133");
		stove.setName("合成炉");
		sub_button.add(stove);
		
		
		ViewButton group = new ViewButton();
		group.setUrl("http://blog.lan-x.cn/archives/73");
		group.setName("公会介绍");
		sub_button.add(group);
		
		ViewButton qa = new ViewButton();
		qa.setUrl("http://blog.lan-x.cn/archives/category/qa");
		qa.setName("常见问题");
		sub_button.add(qa);
		
		complexButton.setSub_button(sub_button);
		button.add(complexButton);
		menu.setButton(button);
		data.put("button", button);
		
		post("http://api.weixin.qq.com/cgi-bin/menu/create", data.toJSONString());
		
	}
	
	public static void getMenu() throws Exception {
		String result = HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=9_HcpZaXjf_ZvDeEG3PLkLCx4XClE5wq7xWDqF2O4RD-xLZmPo6x_JW0XoCT4Wbbz0Dl_sRQQ4vOAppKqgrLVsl0uOf6SqM6Dp37G5178_xkJPFBGI6jxQd4kvD6zRiTssTwtf_uUCHMH_Mc53HEZhAFAOLG");
		System.out.println(result);
	}
	
	
	public static void delconditional() throws Exception{
		JSONObject json = new JSONObject();
		json.put("menuid", "498258915");
		post("https://api.weixin.qq.com/cgi-bin/menu/delconditional", json.toJSONString());
	}
	
	public static void delMenu() throws Exception{
		post("https://api.weixin.qq.com/cgi-bin/menu/delete", "");
	}
	

	public static void getKflist() throws Exception {

		String result =HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=6_PHlTQkClCBMlFx9w2DG2i66mOnak2wcRXAuEuHQM58jkzWjtOaC1Xn3pFljvugozSuaSFMOmsnZDIfYwB2iTDWDkHFLnDT4ixC2WEWuj52zTSVoVyogG3VgKWHPw301VP2W15UJ0n6HawcfMWKWiAHAQEQ");

		System.out.println(result);
	}

	public static  void addKfaccount() throws Exception {

		JSONObject json = new JSONObject();
		json.put("kf_account","songxuan@songxuan_369178133");
		json.put("nickname","客服001");
		json.put("password","123456");
		post("https://api.weixin.qq.com/customservice/kfaccount/add",json.toJSONString());


	}

	public static void inviteworker() throws Exception {

		JSONObject json = new JSONObject();
		json.put("kf_account","songxuan@songxuan_369178133");
		json.put("invite_wx","songxuan_369178133");
		post("https://api.weixin.qq.com/customservice/kfaccount/inviteworker",json.toJSONString());


	}

	public static void createKfSession() throws Exception {
	    JSONObject json = new JSONObject();
	    json.put("kf_account","songxuan@songxuan_369178133");
	    json.put("openid","oMNpa1u91VFvEXuCayn8ssR4uGXI");
	    post("https://api.weixin.qq.com/customservice/kfsession/create",json.toJSONString());

    }

    public static void closeKfSession() throws Exception {

		JSONObject json = new JSONObject();
		json.put("kf_account","songxuan@songxuan_369178133");
		json.put("openid","oMNpa1u91VFvEXuCayn8ssR4uGXI");

		post("https://api.weixin.qq.com/customservice/kfsession/close",json.toJSONString());
	}
    
    
    public static void sendTemplate() throws Exception{
    	JSONObject json = new JSONObject();
    	json.put("touser", "oMNpa1kza1a_Aq51FeLoaXCHRRi4");
    	json.put("template_id", "_kNwxM_GgW0MYMUrHdT_X0Y6Bobww7dC0oBKAGgXUPQ");
    	json.put("url","http://www.lan-x.cn");
    	
    	
    	JSONObject data = new JSONObject();
    	
    	JSONObject first = new JSONObject();
    	first.put("value", "恭喜你完成任务。");
    	first.put("color", "#173177");
    	
    	
    	JSONObject keynote1 = new JSONObject();
    	keynote1.put("value", "这是要替换的内容1");
    	keynote1.put("color", "#173177");
    	
    	JSONObject keynote2 = new JSONObject();
    	keynote2.put("value", "这是要替换的内容2");
    	keynote2.put("color", "#173177");
    	
    	
    	JSONObject remark = new JSONObject();
    	remark.put("value", "这是备注内容");
    	remark.put("color", "#173177");
    	
    	
    	data.put("first", first);
    	data.put("keynote1", keynote1);
    	data.put("keynote2", keynote2);
    	data.put("remark", remark);
    	
    	json.put("data", data);
    	
    	post("https://api.weixin.qq.com/cgi-bin/message/template/send", json.toJSONString());
    }
    
    public static void batchgetMaterial() throws Exception{
    	post("https://api.weixin.qq.com/cgi-bin/material/batchget_material","");
    	
    }
    
    
    public static void addMaterial(){
    	JSONObject json = new JSONObject();
    	
    	
    	
    }
    
    
}
